/**  
 * All rights Reserved, Designed By www.tydic.com
 * @Title:  TaskController.java   
 * @Package com.axin.controller   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: axin     
 * @date:   2019年5月10日 下午11:01:22   
 * @version V1.0 
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
package com.axin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.axin.common.entity.ApiToken;
import com.axin.common.entity.DataGridResult;
import com.axin.common.entity.JsonResult;
import com.axin.common.entity.PageObject;
import com.axin.entity.HbSymbols;
import com.axin.entity.HbTask;
import com.axin.entity.HbUser;
import com.axin.service.CommonService;
import com.axin.service.TaskService;

/**   
 * @Description: TODO 
 * @ClassName:  TaskController
 * @author:  Axin 
 * @date:   2019年5月10日 下午11:01:22   
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
@Controller
@RequestMapping("/task")
public class TaskController {
	
	@Autowired
	private TaskService taskService;
	@Autowired
	private CommonService commonService;
	
	
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@ResponseBody
	public JsonResult save(HbTask task,String symbolValue,HttpServletRequest request){
		HbUser user =(HbUser)request.getAttribute("user");
		String symbol = task.getSymbol();
		if("other".equals(symbol)){
			if(StringUtils.isEmpty(symbolValue)){
				return new JsonResult(new RuntimeException("请选择交易币种。"));
			}else{
				HbSymbols hbSymbols = this.commonService.selectLocalSymbolsBySymbol(symbolValue);
				if(hbSymbols== null){
					return new JsonResult(new RuntimeException("您选择的交易币种不存在选择正确的交易币种。"));
				}
				task.setSymbol(symbolValue);
			}
		}
		
		List<HbTask> list = this.taskService.getUnClosedTaskBySymbol(user.getId(), task.getSymbol());
		if(CollectionUtils.isNotEmpty(list)){
			return new JsonResult(new RuntimeException("您选择的交易币种任务列表中已存在，如要重新创建任务，请先关闭现有任务!"));
		}
		this.taskService.saveTask(user.getId(), task);;
		return new JsonResult();
	}
	
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	@ResponseBody
	public DataGridResult list(PageObject page, HbTask task,HttpServletRequest request){
		HbUser user =(HbUser)request.getAttribute("user");
		task.setUserId(user.getId());
		return this.taskService.getListByParam(page, task);
	}
	
	@RequestMapping(value="/closeTask",method=RequestMethod.POST)
	@ResponseBody
	public JsonResult closeTask(Long id){
		this.taskService.closeTaskById(id);
		return new JsonResult();
	}

	
	@RequestMapping(value="/getTaskById",method=RequestMethod.GET)
	@ResponseBody
	public JsonResult getTaskById(Long id){
		HbTask task = this.taskService.getTaskById(id);
		return new JsonResult(task);
	}
	
	@RequestMapping(value="/updateTaskById",method=RequestMethod.POST)
	@ResponseBody
	public JsonResult updateTaskById(HbTask task,String symbolValue,HttpServletRequest request){
		ApiToken token = (ApiToken)request.getAttribute("apiToken");
		if(token == null){
			return new JsonResult(new RuntimeException("您尚未设置密钥，不能处理您的请求！"));
		}
		this.taskService.updateTaskById(task, token);
		return new JsonResult();
	}
	
	/**
	 * 手动卖出任务的持有金币
	 * @Description: TODO  
	 * @Title: sellById   
	 * @param id
	 * @param request
	 * @return JsonResult
	 * @throws
	 */
	@RequestMapping(value="/sellById",method=RequestMethod.POST)
	@ResponseBody
	public JsonResult sellById(@RequestParam(value="id",required=true)Long id,HttpServletRequest request){
		ApiToken token = (ApiToken)request.getAttribute("apiToken");
		if(token == null){
			return new JsonResult(new RuntimeException("您尚未设置密钥，不能处理您的请求！"));
		}
		HbUser user =(HbUser)request.getAttribute("user");
		this.taskService.sellById(token, user.getId(), id);
		return new JsonResult();
	}
	
}
